﻿Partial Public Class ConnectionPage

    Public WriteOnly Property Redirected() As Boolean
        Set(ByVal value As Boolean)
            blkRedirectedMessage.Visibility = IIf(value, Windows.Visibility.Visible, Windows.Visibility.Collapsed)
        End Set
    End Property


    Dim m_nextpage As EnumPage? = Nothing
    Public WriteOnly Property GoToPageOnConnect() As EnumPage?
        Set(ByVal value As EnumPage?)
            m_nextpage = value
        End Set
    End Property

    Private Sub ConnectionControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loaded
        'If DesignMode Then Return
        If g_connection Is Nothing Then Return

        txtServerName.Text = g_connection.Server
        txtDatabase.Text = g_connection.Database
        Select Case g_connection.Auth
            Case EnumConnectionAuth.eSqlServer : rdoAuthSqlServer.IsChecked = True
            Case Else : rdoAuthWindows.IsChecked = True
        End Select
        txtLogin.Text = g_connection.Login
        txtPassword.Password = g_connection.Password

        SyncFields(g_connection.Auth = EnumConnectionAuth.eSqlServer)

        AddHandler g_connection.ConnectionChanged, AddressOf OnConnectionChanged

    End Sub

    Private Sub btnConnectDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnectDisconnect.Click
        If g_connection.Connected Then
            g_connection.Disconnect()
        Else
            g_connection.Server = txtServerName.Text
            g_connection.Database = txtDatabase.Text
            If rdoAuthSqlServer.IsChecked Then g_connection.Auth = EnumConnectionAuth.eSqlServer
            If rdoAuthWindows.IsChecked Then g_connection.Auth = EnumConnectionAuth.eWindows
            g_connection.Login = txtLogin.Text
            g_connection.Password = txtPassword.Password

            g_connection.Connect()
        End If
    End Sub

    Private Sub OnConnectionChanged()
        ' BUG: THIS WILL HANG THE APPLICATION, LogListener re-entrancy problem?
        Trace.WriteLine("ConnectionControl: ConnectionChanged")
        SyncFields(g_connection.Auth = EnumConnectionAuth.eSqlServer)
        If g_connection.Connected And m_nextpage IsNot Nothing Then g_mainpage.ShowPage(m_nextpage)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        SaveConnectionInfo()
        My.Settings.Save()
    End Sub

    Private Sub btnForgetClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForget.Click
        ForgetConnectionInfo()
        My.Settings.Save()
    End Sub

    Private Sub rdoAuthWindows_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoAuthWindows.Unchecked, rdoAuthWindows.Checked
        SyncFields(rdoAuthSqlServer.IsChecked)
    End Sub

    Private Sub rdoAuthSqlServer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoAuthSqlServer.Unchecked, rdoAuthSqlServer.Checked
        SyncFields(rdoAuthSqlServer.IsChecked)
    End Sub

    Private Sub SyncFields(ByVal sqlserverauth As Boolean)
        txtLogin.IsEnabled = sqlserverauth
        txtPassword.IsEnabled = sqlserverauth
        btnSave.IsEnabled = g_connection.Connected
        btnConnectDisconnect.Content = DirectCast(IIf(g_connection.Connected, "Disconnect", "Connect"), String)
        btnConnectDisconnect.IsEnabled = txtServerName.Text <> "" And txtDatabase.Text <> ""

        'Dim msg As String = ""
        'For Each entry As DictionaryEntry In My.Application.Resources
        '    msg &= entry.Key.GetType.ToString & ": "
        '    msg &= entry.Key.ToString & vbCrLf
        'Next
        'MessageBox.Show(msg)

        If g_connection.Connected Then
            lblConnectionStatus.Content = "You are connected to database '" & g_connection.Database & "'."
            lblConnectionStatus.Style = My.Application.Resources.Item("LabelStatusOkay")
        Else
            lblConnectionStatus.Content = "You are not connected to the database."
            lblConnectionStatus.Style = My.Application.Resources.Item("LabelStatusError")
        End If

    End Sub

    Private Sub txtServerName_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtServerName.KeyUp
        SyncFields(rdoAuthSqlServer.IsChecked)
    End Sub

    Private Sub txtDatabase_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtDatabase.KeyUp
        SyncFields(rdoAuthSqlServer.IsChecked)
    End Sub

    Public Sub SaveConnectionInfo()
        My.Settings.CONNECT_Server = g_connection.Server
        My.Settings.CONNECT_Database = g_connection.Database
        My.Settings.CONNECT_Auth = SpatialAlbum.Core.AuthToString(g_connection.Auth)
        My.Settings.CONNECT_Login = g_connection.Login
        My.Settings.CONNECT_Password = SpatialAlbum.Core.EncryptPassword(g_connection.Password)
        My.Settings.Save()
        Trace.WriteLine("saved connection info")
    End Sub

    Public Sub ForgetConnectionInfo()
        My.Settings.CONNECT_Server = ""
        My.Settings.CONNECT_Database = ""
        My.Settings.CONNECT_Auth = EnumConnectionAuth.eUnknown.ToString
        My.Settings.CONNECT_Login = ""
        My.Settings.CONNECT_Password = ""
        My.Settings.Save()
        Trace.WriteLine("forget (saved empty) connection info")
    End Sub

    Private Sub lnkOptionsPage_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles lnkOptionsPage.Click
        g_mainpage.ShowPage(EnumPage.eOptions)
    End Sub
End Class
